package com.sinoglobal.sinostore.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.lidroid.xutils.BitmapUtils;
import com.orhanobut.logger.Logger;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.SinoStore;
import com.sinoglobal.sinostore.activity.GoodsDetailActivity;
import com.sinoglobal.sinostore.bean.ImageVo;
import com.sinoglobal.sinostore.system.Constants;

import java.util.List;

public class ImagePagerAdapter extends RecyclingPagerAdapter {

    private Context context;

    private List<ImageVo> imageIdList;

    private int size;

    private boolean isInfiniteLoop;

    private BitmapUtils bitmapUtils;

    public ImagePagerAdapter(Context context, List<ImageVo> imageIdList) {
        this.context = context;
        this.imageIdList = imageIdList;
        isInfiniteLoop = false;
        this.size = imageIdList.size();
        bitmapUtils = new BitmapUtils(context);
        bitmapUtils.configDefaultLoadingImage(R.drawable.shop_defaultimghome_content_img_middle);
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.shop_defaultimghome_content_img_middle);
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);

    }

    public void addData(List<ImageVo> imageIdList) {
        if (null != imageIdList) {
            this.imageIdList = imageIdList;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        // Infinite loop
        return isInfiniteLoop ? Integer.MAX_VALUE : imageIdList.size();

    }

    /**
     * get really position
     *
     * @param position
     * @return
     */

    private int getPosition(int position) {
        return isInfiniteLoop ? position % size : position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup container) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = holder.imageView = new ImageView(context);
            holder.imageView.setAdjustViewBounds(true);
            holder.imageView.setScaleType(ScaleType.CENTER_CROP);
            holder.imageView.setLayoutParams(new ViewGroup.LayoutParams(SinoStore.WIDTHPIXELS, (int) (SinoStore.WIDTHPIXELS * (354 / 640.0))));
            holder.imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImageVo imageVo = imageIdList.get(getPosition(position));
                    Logger.i("Adapter参数====" + "&&isInfiniteLoop:" + isInfiniteLoop + "&&size:" + size + "&&position:" + position + "&&getPosition(position):" + getPosition(position));
                    if (imageVo != null) {
                        if (!TextUtils.isEmpty(imageVo.getId())) {
                            if (imageVo != null) {
                                Intent intent = new Intent(context, GoodsDetailActivity.class);
                                intent.putExtra("goodsId", imageVo.getId());
                                context.startActivity(intent);
                            }
                        }
                    }
                }
            });
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        bitmapUtils.display(holder.imageView, Constants.PIC_HOST + imageIdList.get(position % imageIdList.size()).getImg_url());
        return view;

    }

    private static class ViewHolder {
        ImageView imageView;
    }

    /**
     * @return the isInfiniteLoop
     */

    public boolean isInfiniteLoop() {
        return isInfiniteLoop;
    }

    /**
     * @param isInfiniteLoop the isInfiniteLoop to set
     */

    public ImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
        this.isInfiniteLoop = isInfiniteLoop;
        return this;
    }
}
